﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;

namespace Portal
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddDataAccesses(this IServiceCollection services, IConfiguration config)
        {
            services.AddScoped<SqlSugarClient>(serviceProvider =>
            {

                var curDbType = DbType.MySql;

                if (config["DatabaseType"] == "SqlServer")
                {
                    curDbType = DbType.SqlServer;
                }

                var client = new SqlSugarClient(new ConnectionConfig
                {
                    ConnectionString = config.GetConnectionString("DefaultConnectionString"),
                    DbType = curDbType,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute
                });
                client.Aop.OnError = ex =>
                {
                    //此处我们可以记录sql执行的异常信息
                };
                return client;
            });
            
            //此处可以进行数据访问方面的类注入

            return services;
        }
    }
}